const Links = require("../model/links.model.js");
const {Op} = require("sequelize");

class LinksService {
  //保存公司信息，如果没有companyId则为新增，否则为更新
  async saveLinks({ linkId, linkName,lineName,status,statusValue,works,times,cancat,statusType,attenuations }) {
    var update = false;
    var links = {};
    var whereOpt = {};
    if (linkId && linkId > 0) {
      update = true;
      whereOpt.linkId = linkId;
    }
    linkName && Object.assign(links, { linkName });
    lineName && Object.assign(links, { lineName });
    status && Object.assign(links, { status });
    statusValue && Object.assign(links, { statusValue });
    works && Object.assign(links, { works });
    times && Object.assign(links, { times });
    cancat && Object.assign(links, { cancat });
    statusType && Object.assign(links, { statusType });
    attenuations && Object.assign(links, { attenuations });
    if (Object.keys(links).length == 0) {
      return false;
    }
    var res;
    try {
      if (update) {
        res = await Links.update(links, { where: whereOpt });
        if (res[0] > 0) {
          links.linkId = linkId;
          return links;
        } else {
          return false;
        }
      } else {
        res = await Links.create(links);
        if (res.dataValues == undefined) {
          return false;
        } else {
          return res.dataValues;
        }
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  //删除company
  async eraseLinkById(linkId) {
    if (!linkId || linkId <= 0) {
      return false;
    }
    var res;
    try {
      res = Links.destroy({ where: { linkId } });
      if (!res) {
        return false;
      }
      return true;
    } catch (error) {
      console.log(error);
      return null;
    }
  }

  async getLinkByName(linkName){
    if(!linkName){
      return false;
    }
    let rs = await Links.findOne({where:{linkName:linkName}});
    if(!rs){
      return false;
    }else{
      return JSON.parse(JSON.stringify(rs));
    }
  }
  /*
   *查找指定条件的公司
   *@page:分页选项
   *@page.pageIndex:当前第几页
   *@page.pageSize:每页多少条记录
   *@page.pageCount:总共多少页
   *@page.sum:总共多少条记录
   */
  async getLinks({ linkId, linkName,lineName,status,statusValue,works,times,cancat }) {
    var whereOpt = {};
    linkId >= 0 && Object.assign(whereOpt, { linkId });
    linkName && Object.assign(whereOpt, { linkName });
    lineName && Object.assign(whereOpt, { lineName });
    status && Object.assign(whereOpt, { status });
    statusValue && Object.assign(whereOpt, { statusValue });
    works && Object.assign(whereOpt, { works });
    times && Object.assign(whereOpt, { times });
    cancat && Object.assign(whereOpt, { cancat });
    var res;
    try {
      res = await Links.findAll({
        where: whereOpt,
        order: [["linkId", "ASC"]],
      });
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async createWork({linkId,work,works}){
    if(!linkId){
      return false;
    }
    if(!work){
      return false;
    }
    if(!works instanceof Array){
      return;
    }
    works.push(work);
    let list = JSON.stringify(works);
    try {
      var res = await Links.update({works:list},{where:{linkId}});
      if(!res){
        return false;
      }
      return JSON.stringify({linkId,works});
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async eraseWork({linkId,work,works}){
    if(!linkId){
      return false;
    }
    if(!work){
      return false;
    }
    if(!(works instanceof Array) || works.length == 0){
      return;
    }
    for(var i=0;i<works.length;i++){
      if(work == works[i]){
        works.splice(i,1,1);
        break;
      }
    }
    let list = JSON.stringify(works);
    try {
      var res = await Links.update({works:list},{where:{linkId}});
      if(!res){
        return false;
      }
      return JSON.stringify({linkId,works});
    } catch (error) {
      console.log(error);
      return null;
    }
  }

  async getLinksList(linkName){
    if(!linkName){
      return false;
    }
    try {
      var res;
      res = await Links.findAll({attributes:['linkName'],where:{linkId:{[Op.gte]:0}},order:[['linkId',"ASC"]]});
      if(!res){
        return [];
      }
      if(res.length > 0){
        for(let i=0;i<res.length;i++){
          if(linkName == res[i]['linkName']){
            return i;
          }
        }
        return false;
      }else{
        return false;
      }
    } catch (error) {
      console.log(error);
      return false;
    }
  }
}

module.exports = new LinksService();
